﻿// =================================================================== 
// 项目说明
//====================================================================
// wup。@Copy Right 2014
// 文件： news.cs
// 项目名称：买车网
// 创建时间：2014/9/3
// 负责人：wup
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Weixin.Entity.ChinaTax;
using Weixin.Utils;



namespace Weixin.DAL.ChinaTax
{
    /// <summary>
    /// 数据层实例化接口类 dbo.news.
    /// </summary>
    public partial class newsDataAccessLayer 
    {
		#region 构造函数


		/// <summary>
		/// 
		/// </summary>
		public newsDataAccessLayer()
		{
		}
		#endregion

        #region -----------实例化接口函数-----------
		
		#region 添加更新删除
		/// <summary>
		/// 向数据库中插入一条新记录。
		/// </summary>
		/// <param name="_newsModel">news实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Insert(newsEntity _newsModel)
		{
			string sqlStr="insert into news([title],[k_zuozhe],[k_laiyuan],[cont],[id],[id2],[k_shijian],[isTop]) values(@title,@k_zuozhe,@k_laiyuan,@cont,@id,@id2,@k_shijian,@isTop) select @@identity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@title",SqlDbType.VarChar),
			new SqlParameter("@k_zuozhe",SqlDbType.VarChar),
			new SqlParameter("@k_laiyuan",SqlDbType.VarChar),
			new SqlParameter("@cont",SqlDbType.VarChar),
			new SqlParameter("@id",SqlDbType.Int),
			new SqlParameter("@id2",SqlDbType.Int),
			new SqlParameter("@k_shijian",SqlDbType.DateTime),
			new SqlParameter("@isTop",SqlDbType.Int)
			};			
			_param[0].Value=_newsModel.title;
			_param[1].Value=_newsModel.k_zuozhe;
			_param[2].Value=_newsModel.k_laiyuan;
			_param[3].Value=_newsModel.cont;
			_param[4].Value=_newsModel.id;
			_param[5].Value=_newsModel.id2;
			_param[6].Value=_newsModel.k_shijian;
			_param[7].Value=_newsModel.isTop;
			res = Convert.ToInt32(SqlHelper.ExecuteScalar(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param));
			return res;
		}
		/// <summary>
		/// 向数据库中插入一条新记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_newsModel">news实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Insert(SqlTransaction sp,newsEntity _newsModel)
		{
			string sqlStr="insert into news([title],[k_zuozhe],[k_laiyuan],[cont],[id],[id2],[k_shijian],[isTop]) values(@title,@k_zuozhe,@k_laiyuan,@cont,@id,@id2,@k_shijian,@isTop) select @@identity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@title",SqlDbType.VarChar),
			new SqlParameter("@k_zuozhe",SqlDbType.VarChar),
			new SqlParameter("@k_laiyuan",SqlDbType.VarChar),
			new SqlParameter("@cont",SqlDbType.VarChar),
			new SqlParameter("@id",SqlDbType.Int),
			new SqlParameter("@id2",SqlDbType.Int),
			new SqlParameter("@k_shijian",SqlDbType.DateTime),
			new SqlParameter("@isTop",SqlDbType.Int)
			};			
			_param[0].Value=_newsModel.title;
			_param[1].Value=_newsModel.k_zuozhe;
			_param[2].Value=_newsModel.k_laiyuan;
			_param[3].Value=_newsModel.cont;
			_param[4].Value=_newsModel.id;
			_param[5].Value=_newsModel.id2;
			_param[6].Value=_newsModel.k_shijian;
			_param[7].Value=_newsModel.isTop;
			res = Convert.ToInt32(SqlHelper.ExecuteScalar(sp,CommandType.Text,sqlStr,_param));
			return res;
		}


		/// <summary>
		/// 向数据表news更新一条记录。
		/// </summary>
		/// <param name="_newsModel">_newsModel</param>
		/// <returns>影响的行数</returns>
		public int Update(newsEntity _newsModel)
		{
            string sqlStr="update news set [title]=@title,[k_zuozhe]=@k_zuozhe,[k_laiyuan]=@k_laiyuan,[cont]=@cont,[id]=@id,[id2]=@id2,[k_shijian]=@k_shijian,[isTop]=@isTop where newid=@newid";
			SqlParameter[] _param={				
				new SqlParameter("@newid",SqlDbType.Int),
				new SqlParameter("@title",SqlDbType.VarChar),
				new SqlParameter("@k_zuozhe",SqlDbType.VarChar),
				new SqlParameter("@k_laiyuan",SqlDbType.VarChar),
				new SqlParameter("@cont",SqlDbType.VarChar),
				new SqlParameter("@id",SqlDbType.Int),
				new SqlParameter("@id2",SqlDbType.Int),
				new SqlParameter("@k_shijian",SqlDbType.DateTime),
				new SqlParameter("@isTop",SqlDbType.Int)
				};				
				_param[0].Value=_newsModel.newid;
				_param[1].Value=_newsModel.title;
				_param[2].Value=_newsModel.k_zuozhe;
				_param[3].Value=_newsModel.k_laiyuan;
				_param[4].Value=_newsModel.cont;
				_param[5].Value=_newsModel.id;
				_param[6].Value=_newsModel.id2;
				_param[7].Value=_newsModel.k_shijian;
				_param[8].Value=_newsModel.isTop;
			return SqlHelper.ExecuteNonQuery(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param);
		}
		/// <summary>
		/// 向数据表news更新一条记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_newsModel">_newsModel</param>
		/// <returns>影响的行数</returns>
		public int Update(SqlTransaction sp,newsEntity _newsModel)
		{
            string sqlStr="update news set [title]=@title,[k_zuozhe]=@k_zuozhe,[k_laiyuan]=@k_laiyuan,[cont]=@cont,[id]=@id,[id2]=@id2,[k_shijian]=@k_shijian,[isTop]=@isTop where newid=@newid";
			SqlParameter[] _param={				
				new SqlParameter("@newid",SqlDbType.Int),
				new SqlParameter("@title",SqlDbType.VarChar),
				new SqlParameter("@k_zuozhe",SqlDbType.VarChar),
				new SqlParameter("@k_laiyuan",SqlDbType.VarChar),
				new SqlParameter("@cont",SqlDbType.VarChar),
				new SqlParameter("@id",SqlDbType.Int),
				new SqlParameter("@id2",SqlDbType.Int),
				new SqlParameter("@k_shijian",SqlDbType.DateTime),
				new SqlParameter("@isTop",SqlDbType.Int)
				};				
				_param[0].Value=_newsModel.newid;
				_param[1].Value=_newsModel.title;
				_param[2].Value=_newsModel.k_zuozhe;
				_param[3].Value=_newsModel.k_laiyuan;
				_param[4].Value=_newsModel.cont;
				_param[5].Value=_newsModel.id;
				_param[6].Value=_newsModel.id2;
				_param[7].Value=_newsModel.k_shijian;
				_param[8].Value=_newsModel.isTop;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.Text,sqlStr,_param);
		}		
		
		
		/// <summary>
		/// 删除数据表news中的一条记录
		/// </summary>
	    /// <param name="newid">newid</param>
		/// <returns>影响的行数</returns>
		public int Delete(int newid)
		{
			string sqlStr="delete from news where [newid]=@newid";
			SqlParameter[] _param={			
			new SqlParameter("@newid",SqlDbType.Int),
			
			};			
			_param[0].Value=newid;
			return SqlHelper.ExecuteNonQuery(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表news中的一条记录,带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
	    /// <param name="newid">newid</param>
		/// <returns>影响的行数</returns>
		public int Delete(SqlTransaction sp,int newid)
		{
			string sqlStr="delete from news where [newid]=@newid";
			SqlParameter[] _param={			
			new SqlParameter("@newid",SqlDbType.Int),
			
			};			
			_param[0].Value=newid;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.Text,sqlStr,_param);
		}
		#endregion
		
		#region 数据实体
		/// <summary>
		/// 得到  news 数据实体
		/// </summary>
		/// <param name="row">row</param>
		/// <returns>news 数据实体</returns>
		public newsEntity Populate_newsEntity_FromDr(DataRow row)
		{
			newsEntity Obj = new newsEntity();
			if(row!=null)
			{
				Obj.newid = (( row["newid"])==DBNull.Value)?0:Convert.ToInt32( row["newid"]);
				Obj.title =  row["title"].ToString();
				Obj.k_zuozhe =  row["k_zuozhe"].ToString();
				Obj.k_laiyuan =  row["k_laiyuan"].ToString();
				Obj.cont =  row["cont"].ToString();
				Obj.id = (( row["id"])==DBNull.Value)?0:Convert.ToInt32( row["id"]);
				Obj.id2 = (( row["id2"])==DBNull.Value)?0:Convert.ToInt32( row["id2"]);
				Obj.k_shijian = (( row["k_shijian"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( row["k_shijian"]);
				Obj.isTop = (( row["isTop"])==DBNull.Value)?0:Convert.ToInt32( row["isTop"]);
			}
			else
			{
				return null;
			}
			return Obj;
		}
		
        /// <summary>
		/// 得到  news 数据实体
		/// </summary>
		/// <param name="dr">dr</param>
		/// <returns>news 数据实体</returns>
		public newsEntity Populate_newsEntity_FromDr(IDataReader dr)
		{
			newsEntity Obj = new newsEntity();
			
				Obj.newid = (( dr["newid"])==DBNull.Value)?0:Convert.ToInt32( dr["newid"]);
				Obj.title =  dr["title"].ToString();
				Obj.k_zuozhe =  dr["k_zuozhe"].ToString();
				Obj.k_laiyuan =  dr["k_laiyuan"].ToString();
				Obj.cont =  dr["cont"].ToString();
				Obj.id = (( dr["id"])==DBNull.Value)?0:Convert.ToInt32( dr["id"]);
				Obj.id2 = (( dr["id2"])==DBNull.Value)?0:Convert.ToInt32( dr["id2"]);
				Obj.k_shijian = (( dr["k_shijian"])==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime( dr["k_shijian"]);
				Obj.isTop = (( dr["isTop"])==DBNull.Value)?0:Convert.ToInt32( dr["isTop"]);
			
			return Obj;
		}
		#endregion
		
		/// <summary>
		/// 根据ID,返回一个news对象
		/// </summary>
		/// <param name="newid">newid</param>
		/// <returns>news对象</returns>
		public newsEntity Get_newsEntity (int newid)
		{
			newsEntity _obj=null;
			SqlParameter[] _param={
			new SqlParameter("@newid",SqlDbType.Int)
			};
			_param[0].Value=newid;
			string sqlStr="select * from news with(nolock) where newid=@newid";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param))
			{
				while(dr.Read())
				{
            		_obj=Populate_newsEntity_FromDr(dr);
				}
			}
			return _obj;
		}
		/// <summary>
		/// 得到数据表news所有记录
		/// </summary>
		/// <returns>数据集</returns>
		public IList< newsEntity> Get_newsAll()
		{
			IList< newsEntity> Obj=new List< newsEntity>();
			string sqlStr="select * from news";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(WebConfig.ChinaTax,CommandType.Text,sqlStr))
			{
				while(dr.Read())
				{
					Obj.Add(Populate_newsEntity_FromDr(dr));
				}
			}
			return Obj;
		}
		/// <summary>
        /// 检测是否存在根据主键
        /// </summary>
        /// <param name="newid">newid</param>
        /// <returns>是/否</returns>
		public bool IsExistnews(int newid)
		{
			SqlParameter[] _param={
                                      new SqlParameter("@newid",SqlDbType.Int)
                                  };
            _param[0].Value=newid;
            string sqlStr="select Count(*) from news where newid=@newid";
            int a=Convert.ToInt32(SqlHelper.ExecuteScalar(WebConfig.ChinaTax,CommandType.Text,sqlStr,_param));
            if(a>0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        #endregion
		
		#region----------自定义实例化接口函数----------
		#endregion
    }
}
